﻿using System;
using AMM.Business;
using AMM.Business.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class ProductInStoreOrderSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int inStoreTypeValue = Convert.ToInt32(this.cmbInStoreType.SelectedValue);
            int storeBasisValue = Convert.ToInt32(this.cmbStoreBasis.SelectedValue);
            int produceUnitValue = Convert.ToInt32(this.cmbProduceUnit.SelectedValue);
            int orderStatusValue = Convert.ToInt32(this.cmbOrderStatus.SelectedValue);

            ProductInStoreOrderSearchModel searchResult = new ProductInStoreOrderSearchModel();
            searchResult.ProductInStoreOrderCode = this.tbCode.Text;
            searchResult.ProduceConductCode = this.tbProduceConductCode.Text;
            searchResult.ProductInStoreDateStart = this.GetDateTimePickerValue(this.dtpCreateStart);
            searchResult.ProductInStoreDateEnd = this.GetDateTimePickerValue(this.dtpCreateEnd);
            searchResult.ProductCode = this.tbProductCode.Text;
            searchResult.ProductInStoreTypeId = inStoreTypeValue;
            searchResult.StoreBasisId = storeBasisValue;
            searchResult.ProduceUnitId = produceUnitValue;
            searchResult.OrderStatus = orderStatusValue;

            if (inStoreTypeValue == 0) { searchResult.ProductInStoreTypeId = null; }
            if (storeBasisValue == 0) { searchResult.StoreBasisId = null; }
            if (produceUnitValue == 0) { searchResult.ProduceUnitId = null; }
            if (orderStatusValue == -1) { searchResult.OrderStatus = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbCode.Clear();
            this.tbProduceConductCode.Clear();
            this.tbProductCode.Clear();
            this.dtpCreateStart.Checked = false;
            this.dtpCreateEnd.Checked = false;
            this.cmbInStoreType.SelectedIndex = 0;
            this.cmbStoreBasis.SelectedValue = 0;
            this.cmbProduceUnit.SelectedIndex = 0;
            this.cmbOrderStatus.SelectedIndex = 0;
        }

        protected void BindStoreBasis() {
            this.cmbStoreBasis.DataSource = new StoreBasisProcessor().FetchEnabledStoreBasis();
            this.cmbStoreBasis.DisplayMember = "Code";
            this.cmbStoreBasis.ValueMember = "StoreBasisId";
        }

        public ProductInStoreOrderSearchControl() {
            this.InitializeComponent();            
        }

        private void ProductInStoreOrderSearchControl_Load(object sender, EventArgs e) {
            this.BindBasics(this.cmbProduceUnit, BasicsType.ProduceUnit);
            this.BindBasics(this.cmbInStoreType, BasicsType.ProductInStoreType);
            this.BindAuditStatus(this.cmbOrderStatus);
            this.BindStoreBasis();
        }
    }
}
